package org.tio.site.http;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.http.common.HttpConst;
import org.tio.http.common.HttpRequest;
import org.tio.http.common.HttpResponse;
import org.tio.http.common.RequestLine;
import org.tio.http.server.intf.HttpServerInterceptor;

/**
 * @author tanyaowu
 * 2017年8月3日 下午1:27:05
 */
public class TioSiteHttpServerInterceptor implements HttpServerInterceptor {
	private static Logger log = LoggerFactory.getLogger(TioSiteHttpServerInterceptor.class);

	public static final TioSiteHttpServerInterceptor me = new TioSiteHttpServerInterceptor();

	/**
	 * @param args
	 * @author tanyaowu
	 */
	public static void main(String[] args) {

	}

	/**
	 *
	 * @author tanyaowu
	 */
	private TioSiteHttpServerInterceptor() {
	}

	/**
	 * @param packet
	 * @param requestLine
	 * @param httpResponse
	 * @throws Exception
	 * @author tanyaowu
	 */
	@Override
	public void doAfterHandler(HttpRequest packet, RequestLine requestLine, HttpResponse httpResponse) throws Exception {
		if (packet.getIsAjax()) {
			httpResponse.addHeader(HttpConst.ResponseHeaderKey.Access_Control_Allow_Origin, "*");
			httpResponse.addHeader(HttpConst.ResponseHeaderKey.Access_Control_Allow_Headers, "x-requested-with,content-type");
		}
	}

	/**
	 * @param packet
	 * @param requestLine
	 * @return
	 * @throws Exception
	 * @author tanyaowu
	 */
	@Override
	public HttpResponse doBeforeHandler(HttpRequest packet, RequestLine requestLine, HttpResponse httpResponseFromCache) throws Exception {
		return null;
	}
}
